package com.jack.thinking.thread.core._2;

public class DeadThreadClass {
	public static void main(String[] args) throws InterruptedException {
		MyDeadObject md =  new MyDeadObject();
		Thread t1 = new Thread(md);
		t1.setName("A");
		md.setThreadName("A");
		t1.start();
		//Thread.sleep(100);
		Thread t2 = new Thread(md);
		t2.setName("B");
		md.setThreadName("B");
		t2.start();
	}
}


class MyDeadObject implements Runnable{
	private Object o1 = new Object();
	private Object o2 = new Object();
	private String threadName;
	
	public String getThreadName() {
		return threadName;
	}

	public void setThreadName(String threadName) {
		this.threadName = threadName;
	}

	@Override
	public void run() {
		if("A".equals(threadName)) {
			synchronized (o1) {
				System.out.println(Thread.currentThread().getName()+ "-----start o1");
				try {
					Thread.sleep(1000L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+ "-----end o1");
				synchronized(o2) {
					System.out.println(Thread.currentThread().getName()+ "-----start inner o1-o2");
				}
			}
		}
		if("B".equals(threadName)) {
			synchronized (o2) {
				System.out.println(Thread.currentThread().getName()+ "-----start o2");
				try {
					Thread.sleep(1000L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+ "-----end o2");
				synchronized(o1) {
					System.out.println(Thread.currentThread().getName()+ "-----start inner o2-o1");
				}
			}
		}
	}
}